﻿/// <reference path="MouseButton.js" />

var Events = function()
{
}


Events.AddRaw = function AddRaw(obj, type, fnToCall)
{
	if (obj.attachEvent !== undefined)
		obj.attachEvent("on" + type, fnToCall);
	else
		obj.addEventListener(type, fnToCall, false);
}
Events.RemoveRaw = function RemoveRaw(obj, type, fnToCall)
{
	if (obj.detachEvent)
		obj.detachEvent("on" + type, fnToCall);
	else
		obj.removeEventListener(type, fnToCall, false);
}


Events.GetSender = function GetSender(e)
{
	if (typeof (e.currentTarget) !== "undefined")
		return e.currentTarget;
	if (typeof (Element) != "undefined")
	{
		if (Element.applyAllProperties)
			Element.applyAllProperties(e.srcElement);
	}
	return e.srcElement;
}
Events.GetOrigSender = function GetOrigSender(e)
{
	if (e.fromElement)
	{
		if (typeof (Element) != "undefined")
		{
			if (Element.applyAllProperties)
				Element.applyAllProperties(e.srcElement);
		}
		return e.fromElement;
	}
	else
		return e.originalTarget;
}
Events.Stop = function Stop(e)
{
	e.cancelBubble = true;
	if (e.stopPropagation)
		e.stopPropagation();
}
Events.Cancel = function Cancel(e)
{
	e.returnValue = false;
	if (e.preventDefault != null)
		e.preventDefault();
}
Events.IsCancelable = function IsCancelable(e)
{
	if (e.cancelable !== null && e.cancelable !== undefined)
		return e.cancelable;
	return true;
}
Events.GetKeyCode = function GetKeyCode(e)
{
	if (e.keyCode)
		return e.keyCode;
	if (e.which)
		return e.which;
	return null;
}
Events.GetMouseCoords = function GetMouseCoords(e)
{
	return new Point(e.clientX, e.clientY);
}
Events.GetMouseCoordsElement = function GetMouseCoordsElement(e)
{
	if (e.layerX)
		return new Point(e.layerX - e.currentTarget.offsetLeft, e.layerY - e.currentTarget.offsetTop);
	return new Point(e.offsetX, e.offsetY);
}
Events.GetMouseCoordsScreen = function GetMouseCoordsScreen(e)
{
	if (navigator.appName == "Microsoft Internet Explorer")
		return new Point(e.screenX - 3, e.screenY - 3);
	return new Point(e.screenX, e.screenY);
}
Events.GetMouseButton = function GetMouseButton(e)
{
	if (e.which !== undefined)
	{
		switch (e.which)
		{
			case 1:
				return MouseButton.Left;
			case 2:
				return MouseButton.Middle;
			case 3:
				return MouseButton.Right;
		}
	}
	return e.button;
}
